home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1308.dms / var1308.adf / DNET2_10.LHA / DNet / Amiga / Sourcen.lha / client / loadav.c < prev    next >
C/C++ Source or Header  |  1993-01-14  |  6KB  |  300 lines

  1.  
  2. /*
  3.  *  LOADAV.C
  4.  *
  5.  *  DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved.
  6.  *
  7.  *  LOADAV  [frequency]     Connect to remote UNIX system and display
  8.  *                load average.  Default is every 5 minutes.
  9.  *
  10.  *  frequency in seconds, default is every 60 seconds
  11.  *
  12.  */
  13.  
  14. #include "defs.h"
  15. #include <local/deemu.h>
  16.  
  17. int __stdargs CXBRK(void);
  18.  
  19. short Deemu[] = {
  20.     DMSTRT, 0, 0,
  21.     DMNW  , 0, 10, 2, 2, -80, 40, 0xFFFF,
  22.     DMEND , 0, 0
  23. };
  24.  
  25. #define DMNWOFF 4
  26.  
  27. #define NA  0
  28. #define GWIDTH    (sizeof(Graph)/sizeof(Graph[0]))
  29. #define GDEPTH    (sizeof(GMap)/sizeof(GMap[0]))
  30. #define GMASK    (GWIDTH-1)
  31.  
  32. uword    GMax[] = { 2*256, 50 };
  33. uword    GIncr[]= { 2*256, 20 };
  34. uword    GMap[2] = { 0, 1 };
  35. uword    Graph[1024][GDEPTH];    /*  5Min,#users.       */
  36. uword    Gi;
  37.  
  38. ubyte    Scr[128];
  39. ubyte    Initial;
  40.  
  41. ubyte Title[128];
  42.  
  43. extern void updatewindow();
  44.  
  45. NW Nw = {
  46.     0, 0, 320, 50, -1, -1,
  47.     NEWSIZE|CLOSEWINDOW,
  48.     WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|NOCAREREFRESH,
  49.     NULL, NULL, Title, NULL, NULL,
  50.     32, 18, -1, -1, WBENCHSCREEN
  51. };
  52.  
  53. WIN *Win;
  54. RP  *Rp;
  55.  
  56. extern int Enable_Abort;
  57.  
  58. struct IntuitionBase *IntuitionBase;
  59. struct GfxBase *GfxBase;
  60.  
  61. void clearwindow ARGS((void));
  62. void updatewindow ARGS((char *));
  63. void main ARGS((int, char **));
  64.  
  65. int
  66. brk()
  67. {
  68.     return(0);
  69. }
  70.  
  71. void
  72. main(ac,av)
  73. char *av[];
  74. {
  75.     void *chan = NULL;
  76.     short numsecs = 60;
  77.     long imask, tmask, dmask, mask;
  78.     char notdone = 1;
  79.     char *host = NULL;
  80.     PORT *TimPort = CreatePort(NULL, 0);
  81.     IOT Iot;
  82.  
  83.     onbreak(brk);
  84.     sprintf(Title, "LoadAv V%s%s", VERSION, LOADAV_VERSION);
  85.     {
  86.     short i;
  87.     for (i = 1; i < ac; ++i) {
  88.         if (strncmp(av[i], "-N", 2) == 0) {
  89.         host = av[i]+2;
  90.         continue;
  91.         }
  92.         numsecs = atoi(av[i]);
  93.     }
  94.     }
  95.  
  96.  
  97.     Iot.tr_node.io_Device = NULL;
  98.  
  99.     if (OpenDevice("timer.device", UNIT_VBLANK, (IOR *)&Iot, 0))
  100.     goto fail;
  101.     Iot.tr_node.io_Command = TR_ADDREQUEST;
  102.     Iot.tr_node.io_Message.mn_ReplyPort = TimPort;
  103.     Iot.tr_time.tv_micro = 1;
  104.     Iot.tr_time.tv_secs  = 0;
  105.     SendIO((IOR *)&Iot);
  106.  
  107. #ifndef LATTICE
  108.     Enable_Abort = 0;
  109. #endif
  110.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
  111.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0);
  112.  
  113.     chan = DOpen(host, PORT_LOADAV, 25, 25);
  114.     if (chan == NULL) {
  115.     puts("no connect");
  116.     goto fail;
  117.     }
  118.     InitDeemuNW(Deemu+DMNWOFF, &Nw);
  119.     Win = OpenWindow(&Nw);
  120.     if (Win == NULL) {
  121.     puts("Unable to open window");
  122.     goto fail;
  123.     }
  124.     Rp = Win->RPort;
  125.     imask   = 1 << Win->UserPort->mp_SigBit;
  126.     dmask   = 1 << ((PORT *)chan)->mp_SigBit;
  127.     tmask   = 1 << TimPort->mp_SigBit;
  128.  
  129.     clearwindow();
  130.     while (notdone) {
  131.     mask = Wait(imask|dmask|tmask|SIGBREAKF_CTRL_C);
  132.     if (mask & SIGBREAKF_CTRL_C)
  133.         notdone = 0;
  134.     if (mask & imask) {
  135.         IMESS *im;
  136.         while (im = (IMESS *)GetMsg(Win->UserPort)) {
  137.         switch(im->Class) {
  138.         case NEWSIZE:
  139.             clearwindow();
  140.             break;
  141.         case CLOSEWINDOW:
  142.             notdone = 0;
  143.             break;
  144.         }
  145.         ReplyMsg((MSG *)im);
  146.         }
  147.     }
  148.     if (mask & dmask) {
  149.         char dummy;
  150.         if ((dummy = DNRead(chan, &dummy, 1)) != 0)
  151.         notdone = 0;
  152.     }
  153.     while (mask & tmask) {      /*  while just so we can break */
  154.         char len = 0;
  155.  
  156.         if (GetMsg(TimPort)) {
  157.         Iot.tr_time.tv_micro = 0;
  158.         Iot.tr_time.tv_secs  = numsecs;
  159.         SendIO((IOR *)&Iot);
  160.         if (DWrite(chan, &len, 1) == 1 && DRead(chan, &len, 1) == 1) {
  161.             if (len < sizeof(Title) && DRead(chan, Title, len) == len) {
  162.             Title[len] = 0;
  163.             updatewindow(Title);
  164.             SetWindowTitles(Win, Title, (char *)-1);
  165.             break;
  166.             }
  167.         }
  168.         notdone = 0;
  169.         }
  170.         break;
  171.     }
  172.     }
  173.  
  174. fail:
  175.     if (Iot.tr_node.io_Device) {
  176.     AbortIO((IOR *)&Iot);
  177.     WaitIO((IOR *)&Iot);
  178.     CloseDevice((IOR *)&Iot);
  179.     }
  180.     DeletePort(TimPort);
  181.     if (Win)
  182.     CloseWindow(Win);
  183.     if (chan)
  184.     DClose(chan);
  185.     if (IntuitionBase)
  186.     CloseLibrary((LIB *)IntuitionBase);
  187.     if (GfxBase)
  188.     CloseLibrary((LIB *)GfxBase);
  189. }
  190.  
  191. /*
  192.  *  Graphics routines.    ************************************************
  193.  */
  194.  
  195. short WOx, WOy, Ww, Wh;
  196.  
  197. void
  198. clearwindow()
  199. {
  200.     short i, j, d;
  201.  
  202.     WOx = Win->BorderLeft;
  203.     WOy = Win->BorderTop;
  204.     Ww    = Win->Width - Win->BorderRight - Win->BorderLeft;
  205.     Wh    = Win->Height- Win->BorderTop    - Win->BorderBottom;
  206.  
  207.     SetAPen(Rp, 0);
  208.     RectFill(Rp, WOx, WOy, Ww + WOx, Wh + WOy);
  209.     WOx += 2;
  210.     WOy += 2;
  211.     Ww    -= 4;
  212.     Wh    -= 4;
  213.  
  214.     /*
  215.      *    Redraw the graph.  Scale values relative to GMax[?] and Wh.
  216.      *
  217.      *    ypos = (WOy + Wh) - (Wh * value / GMax[d])
  218.      *
  219.      */
  220.  
  221.     for (d = 0; d < GDEPTH; ++d) {
  222.     char move = 1;
  223.     SetAPen(Rp, (d & 1) ? 3 : 1);
  224.     for (i = Ww - 1, j = Gi - 1; i >= 0; --i, --j) {
  225.         uword value = Wh * Graph[j&GMASK][d] / GMax[GMap[d]];
  226.         (move) ?
  227.         Move(Rp, WOx + i, WOy + Wh - value - 1) :
  228.         Draw(Rp, WOx + i, WOy + Wh - value - 1)
  229.         ;
  230.         move = 0;
  231.     }
  232.     }
  233. }
  234.  
  235. /*
  236.  *  5:44pm up 2 days, 22:30, 37 users, load average: 5.98, 7.93, 7.97
  237.  *              7 mins,
  238.  */
  239.  
  240. void
  241. updatewindow(str)
  242. char *str;
  243. {
  244.     short d;
  245.     char refresh = 0;
  246.     uword ary[GDEPTH];
  247.  
  248.     {
  249.     long nusers, i1, f1, i5, f5;
  250.     char *ptr = str;
  251.  
  252.     while (strncmp(ptr, "users", 5) && *ptr)
  253.         ++ptr;
  254.     while (*--ptr == ' ' && ptr != str)
  255.         --ptr;
  256.     while (*--ptr != ' ' && ptr != str)
  257.         --ptr;
  258.     nusers = atoi(ptr+1);
  259.     while (strncmp(ptr, "load", 4) && *ptr)
  260.         ++ptr;
  261.     sscanf(ptr, "load average: %ld.%ld, %ld.%ld,",
  262.         &i1, &f1, &i5, &f5
  263.     );
  264.     /*
  265.     ary[0] = (i1 << 8) | ((f1 << 8) / 100);
  266.     */
  267.     ary[0] = (i5 << 8) | ((f5 << 8) / 100);
  268.     ary[1] = nusers;
  269.     }
  270.     for (d = 0; d < GDEPTH; ++d) {
  271.     while (ary[d] > GMax[GMap[d]] && ary[d] < 65000) {
  272.         GMax[GMap[d]] += GIncr[GMap[d]];
  273.         refresh = 1;
  274.     }
  275.     Graph[Gi][d] = ary[d];
  276.     }
  277.     if (!Initial) {
  278.     short i;
  279.     Initial = 1;
  280.     for (i = 0; i < GWIDTH; ++i) {
  281.         for (d = 0; d < GDEPTH; ++d)
  282.         Graph[i][d] = Graph[Gi][d];
  283.     }
  284.     }
  285.     Gi = (Gi + 1) & GMASK;
  286.     if (refresh) {
  287.     clearwindow();
  288.     return;
  289.     }
  290.     ScrollRaster(Rp, 1, 0, WOx, WOy, WOx + Ww - 1, WOy + Wh - 1);
  291.     for (d = 0; d < GDEPTH; ++d) {
  292.     uword value1 = Wh * Graph[(Gi-2)&GMASK][d] / GMax[GMap[d]];
  293.     uword value2 = Wh * Graph[(Gi-1)&GMASK][d] / GMax[GMap[d]];
  294.     SetAPen(Rp, (d & 1) ? 3 : 1);
  295.     Move(Rp, Ww + WOx - 2, WOy + Wh - value1 - 1);
  296.     Draw(Rp, Ww + WOx - 1, WOy + Wh - value2 - 1);
  297.     }
  298. }
  299.  
  300.